// 1、初始化  获取收藏数据
hxCollect();
function hxCollect() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/user/userDataList',
    headers: {
      token: localStorage.getItem('token'),
    },
    params: {
      type: 1,
      page: 1,
      limit: 5,
    },
  }).then(function (res) {
    console.log(res);
    if (res.status !== 200) {
      return;
    }
    // console.log(res.data.data.list.data);
    // 渲染数据
    collectData(res.data.data.list.data);
  });
}
// 2、封装渲染数据
var str;
function collectData(data) {
  if (data.length <= 0) {
    str = `<p style="font-size='20px'">您还未收藏</p>`;
  } else {
    str = data
      .map(item => {
        var time = getdate(item.create_date);
        return `
        <article class="excerpt excerpt-one">
        <header>
          <a class="cat label label-important" href="#">${item.catename}<i class="label-arrow"></i></a>
          <h2>
            <a id="hx_tz" data-id="${item.id}"  target="_blank" href="show.html" title="阿里云拼团推荐服务器">${item.title}</a>
          </h2>
        </header>
        <p class="text-muted time">
          <a href="#">${item.author}</a> 发布于${time}
        </p>
        <p class="focus">
          <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
            <span class="item"><span class="thumb-span">
            <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" /></span></span></a>
        </p>
        <p class="note">${item.description}</p>
        <p class="text-muted views">
          <span class="post-views">阅读(${item.click})</span>
          <span class="post-comments">评论(0)</span>
          <a href="javascript:;" class="post-like">
            <i class="fa fa-thumbs-up"></i>赞 (<span>${item.lovenum}</span>)
          </a>
          <span class="post-tags">标签：
            <a href="#" rel="tag">${item.keywords}</a> /
            <a href="#" rel="tag">阿里云</a></span>
             <button class="btn btn-xs btn-danger" data-id="${item.id}"><i class="fa fa-trash"></i>&nbsp;取消收藏</button>
        </p>
      </article>
      `;
      })
      .join('');
  }

  $('.list-box').html(str);
}

// 处理时间戳
function getdate() {
  var now = new Date(),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
}

// 点击收藏 跳转到收藏页面
$('body').on('click', '#hx_tz', function (e) {
  e.preventDefault();
  window.open('show.html?id=' + $(this).attr('data-id'));
});

// 3、点击 删除收藏
$('body').on('click', '.btn-danger', function () {
  //   action=del
  var id = $(this).attr('data-id');
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home//user/userDataHandle',
    headers: {
      token: localStorage.getItem('token'),
    },
    params: {
      type: 1,
      article_id: id,
      action: 'del',
    },
  }).then(function (res) {
    console.log(res);
    hxCollect();
  });
});
homePage();
// 首页和全局数据
function homePage() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/index/index',
  }).then(function (res) {
    // console.log(res);
    // 导航栏
    let strNav = res.data.data.allCate
      .map(item => {
        return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
      })
      .join('');
    $('#nav').append(strNav);
  });
}

userCenter();
// 用户中心和退出
function userCenter() {
  // 没有token的时候隐藏用户中心和退出
  if (localStorage.getItem('token')) {
    let str = `
         <a href="user.html" id="user"><i class="fa fa-user"></i>&nbsp;用户中心</a>
         <a href="javascript:;" class="quit"><i></i>&nbsp;退出</a>`;
    $('.slinks').html(str);
  } else {
    let str = `
          <a href="login.html"><i class="fa fa-hand-o-right"></i>&nbsp;登录</a> |
          <a href="reg.html"><i class="fa fa-registered"></i>&nbsp;注册</a>`;
    $('.slinks').html(str);
  }
  $('#user').on('click', function (e) {
    e.preventDefault();
    if (localStorage.getItem('token')) {
      location.href = 'user.html';
    } else {
      location.href = 'login.html';
    }
  });
  $('.quit').on('click', function (e) {
    layer.confirm('你确定要退出吗?', { icon: 3, title: '退出提示' }, function (index) {
      localStorage.removeItem('token');
      location.reload();

      layer.close(index);
    });
  });
}
// 导航栏跳转
function getNavSkip() {
  $('#nav').on('click', 'a', function (e) {
    e.preventDefault();
    window.open('../list.html?id=' + $(this).attr('id'));
  });
}
getNavSkip();
